package com.example.permission.myjbm.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.permission.myjbm.adapter.CertificateAdapter;
import com.example.permission.myjbm.dao.BaseAty;
import com.example.permission.myjbm.model.CertificateModel;
import com.example.permission.myjbm.net.ReleaseNet;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jbm.jbmsupplier.util.IExChangeCtrl;
import com.jbm.jbmsupplier.util.VoucherEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 礼券中心
 */
public class CertificateCenterAty extends BaseAty {
    //返回
    @InjectView(R.id.header_back)
    ImageView headerBack;
    //标题
    @InjectView(R.id.header_title)
    TextView headerTitle;
    //提交
    @InjectView(R.id.header_right)
    TextView headerRight;
    //listView
    @InjectView(R.id.certificate_pull)
    PullToRefreshListView certificatePull;
    //默认图
    @InjectView(R.id.lv_default)
    ImageView lvDefault;
    private CertificateModel model;
    private List<VoucherEntity> data;
    private CertificateAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_certificate_center_aty);
        ButterKnife.inject(this);
        initView();
    }
    //初始化数据
    private void initView() {
        data=new ArrayList<VoucherEntity>();
     model=new CertificateModel(headerRight,headerTitle,handlerAty);

        model.setListener(certificatePull);

/**
 *   Intent intent=new Intent(c, IntegralDetail.class);
 intent.putExtra("isDetial",1);
 c.startActivity(intent);

 */
    }

    @Override
    protected void onResume() {
        super.onResume();
        model.setPage(0);
        baseLoader.show();
        model.addNet();
    }

    @Override
    protected void netHandler(int what,Object obj){
        switch (what){
            case ReleaseNet.DUIHUANERROR:
                IExChangeCtrl ob=(IExChangeCtrl)obj;
                List<VoucherEntity> data1 = ob.voucherList;
                if(model.getPage()==0){
                    if(data1.size()==0){
                        lvDefault.setVisibility(View.VISIBLE);
                    }else{
                        lvDefault.setVisibility(View.GONE);
                    }
                    data.clear();
                    data.addAll(data1);
                    adapter=new CertificateAdapter(data,CertificateCenterAty.this);
                    certificatePull.setAdapter(adapter);
                }else{
                    data.addAll(data1);
                    adapter.setNotify((ArrayList) data);
                }
                certificatePull.onRefreshComplete();
                break;
        }

    }




    @OnClick(R.id.header_back)
    public void onClick() {
       finish();
    }
}
